package com.wsm.oa.service.impl;

import com.wsm.common.utils.ShiroUtils;
import com.wsm.common.utils.SnowFlake;
import com.wsm.oa.mapper.NotifyRecordDao;
import com.wsm.oa.domain.NotifyRecordDO;
import com.wsm.oa.service.NotifyRecordService;
import com.wsm.system.domain.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class NotifyRecordServiceImpl implements NotifyRecordService {
	@Autowired
	private NotifyRecordDao notifyRecordDao;
	@Autowired
	private SimpMessagingTemplate template;

	@Override
	public NotifyRecordDO get(Long id) {
		return notifyRecordDao.get(id);
	}

	@Override
	public List<NotifyRecordDO> list(Map<String, Object> map) {
		return notifyRecordDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return notifyRecordDao.count(map);
	}
	
	@Override
	public int save(NotifyRecordDO notifyRecord) {
		notifyRecord.setId(SnowFlake.nextId());
		return notifyRecordDao.insert(notifyRecord);
	}
	
	@Override
	public int update(NotifyRecordDO notifyRecord){
		return notifyRecordDao.orUpdate(notifyRecord);
	}
	
	@Override
	public int remove(Long id){
		return notifyRecordDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return notifyRecordDao.batchRemove(ids);
	}

	@Override
	@Async
	public void changeRead(NotifyRecordDO notifyRecord) {
		notifyRecordDao.changeRead(notifyRecord);
		UserDO user = ShiroUtils.getUser();
		template.convertAndSendToUser(user.toString(), "/queue/cleannotifications", "消息已读");
	}

}
